﻿package org.xhs.report.doc.ui
{
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.ui.*;
    import baidu.wenku.common.utils.*;
    import baidu.wenku.doc.*;
    import baidu.wenku.doc.model.*;
    import baidu.wenku.doc.utils.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.text.*;
    import flash.ui.*;
    import flash.utils.*;

    public class ViewPort extends Sprite
    {
        protected var _snapText:TextSnapshot;
        protected var _snapText0:TextSnapshot;
        protected var _snapText1:TextSnapshot;
        protected var _snapText2:TextSnapshot;
        protected var _resultNum:int;
        protected var _searchPageth:int = -1;
        protected var _mouseDown:Boolean = false;
        protected var _pagethWhenMouseDown:int;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var screenModel:ScreenModel;
        protected var errorSprite:ErrorSprite;
        protected var prevScreenBtn:ScreenButton;
        protected var nextScreenBtn:ScreenButton;
        protected var docPageFlip:DocPageFlip;
        protected var docLocatModel:DocLocatModel;
        protected var zoomModel:DocZoomModel;
        protected var thisViewer:IDocViewer;

        public function ViewPort()
        {
            this.initModel();
            this.initContModel();
            return;
        }// end function

        protected function initModel() : void
        {
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            this.zoomModel = InstanceManager.createSingletonInstance(DocZoomModel);
            this.zoomModel.addEventListener(DocZoomModel.ZOOM_CHANGED, this.zoomChanged);
            this.errorSprite = new ErrorSprite();
            this.prevScreenBtn = new ScreenButton(false);
            this.prevScreenBtn.addEventListener(MouseEvent.CLICK, this.previousScreenButtonDown);
            this.nextScreenBtn = new ScreenButton(true);
            this.nextScreenBtn.addEventListener(MouseEvent.CLICK, this.nextScreenButtonDown);
            return;
        }// end function

        protected function initContModel() : void
        {
            this.docLocatModel = InstanceManager.createSingletonInstance(DocLocatModel);
            this.docLocatModel.addEventListener(LocatEvent.PAGE_LOADED, function (event:LocatEvent) : void
            {
                dispatchEvent(event);
                return;
            }// end function
            );
            this.docLocatModel.addEventListener(LocatEvent.PROCESS_DEL_DOC, function (event:LocatEvent) : void
            {
                processDeletedDoc();
                return;
            }// end function
            );
            this.docLocatModel.addEventListener(LocatEvent.PROCESS_NO_DOC, function (event:LocatEvent) : void
            {
                processNoDoc();
                return;
            }// end function
            );
            this.docLocatModel.addEventListener(LocatEvent.START_LOADING, this.onStartLoading);
            this.docLocatModel.addEventListener(LocatEvent.SHOW_LOADING, function (event:LocatEvent) : void
            {
                dispatchEvent(event);
                return;
            }// end function
            );
            this.docLocatModel.addEventListener(LocatEvent.STOP_LOADING, function (event:LocatEvent) : void
            {
                dispatchEvent(event);
                return;
            }// end function
            );
            this.docLocatModel.addEventListener(LocatEvent.BLANKPAGE_ADDED, this.blankpageAdded);
            this.thisViewer = InstanceManager.createSingletonInstance(this.loaderInfoVo.isPPT ? (PPTViewer) : (SingleViewer));
            this.thisViewer.addEventListener(LocatEvent.SCROLL_POS_CHANGE, this.scrollPosChange);
            this.thisViewer.addEventListener(LocatEvent.ZOOM_CHANGED, this.zoomChange);
            addChild(this.thisViewer as DisplayObject);
            return;
        }// end function

        protected function screenResize(event:Event = null) : void
        {
            if (this.screenModel.isFullscreen)
            {
                this.screenModel.stage.addEventListener(MouseEvent.MOUSE_WHEEL, this.doStageMouseWheelInFull);
            }
            else
            {
                this.screenModel.stage.removeEventListener(MouseEvent.MOUSE_WHEEL, this.doStageMouseWheelInFull);
            }
            return;
        }// end function

        protected function onStartLoading(event:LocatEvent) : void
        {
            dispatchEvent(new LocatEvent(LocatEvent.SHOW_LOADING));
            return;
        }// end function

        protected function zoomChange(event:LocatEvent) : void
        {
            this.resetScreenButton();
            if (this.thisViewer.horizontalScrollVisible)
            {
                this.nextScreenBtn.y = this.screenModel.stageHeight - Config.SB_HEIGHT - this.nextScreenBtn.height - Config.TB_HEIGHT + 1 - Config.DV_WIDTH_RIGHT_SCROLLBAR;
            }
            else
            {
                this.nextScreenBtn.y = this.screenModel.stageHeight - Config.SB_HEIGHT - this.nextScreenBtn.height - Config.TB_HEIGHT + 1;
            }
            return;
        }// end function

        protected function blankpageAdded(event:LocatEvent) : void
        {
            this.thisViewer.data = this.docLocatModel.pagesArray;
            this.resetScreenButton();
            return;
        }// end function

        protected function scrollPosChange(event:LocatEvent = null) : void
        {
            if (DocConfig.pageMode == DocConfig.PAGE_MODE_DOUBLE || this.loaderInfoVo.isPPT)
            {
                return;
            }
            var _loc_2:* = Math.ceil(this.docLocatModel.pagesAllAvailable * (this.thisViewer.vScrollPosition + this.screenModel.stageHeight / 2) / (this.thisViewer.maxVScrollPosition + this.thisViewer.vScrollbarHeight)) - 1;
            this.docLocatModel.docScroll(_loc_2);
            this.resetScreenButton();
            this.jumpToReader();
            return;
        }// end function

        protected function resetScreenButton() : void
        {
            if (DocConfig.pageMode == DocConfig.PAGE_MODE_DOUBLE || this.loaderInfoVo.isPPT)
            {
                if (contains(this.prevScreenBtn))
                {
                    removeChild(this.prevScreenBtn);
                }
                if (contains(this.nextScreenBtn))
                {
                    removeChild(this.nextScreenBtn);
                }
                return;
            }
            if (this.scrollPosition > 0)
            {
                addChild(this.prevScreenBtn);
            }
            else if (contains(this.prevScreenBtn))
            {
                removeChild(this.prevScreenBtn);
            }
            if (this.scrollPosition < this.scrollMax)
            {
                addChild(this.nextScreenBtn);
            }
            else if (contains(this.nextScreenBtn))
            {
                removeChild(this.nextScreenBtn);
            }
            return;
        }// end function

        protected function processNoDoc() : void
        {
            this.thisViewer.destroy();
            dispatchEvent(new LocatEvent(LocatEvent.STOP_LOADING));
            this.errorSprite.processNoDoc();
            dispatchEvent(new LocatEvent(LocatEvent.PAGE_LOADED));
            return;
        }// end function

        protected function processDeletedDoc() : void
        {
            this.thisViewer.destroy();
            dispatchEvent(new LocatEvent(LocatEvent.STOP_LOADING));
            this.errorSprite.processDeletedDoc();
            dispatchEvent(new LocatEvent(LocatEvent.PAGE_LOADED));
            return;
        }// end function

        protected function jumpToReader() : void
        {
            var xid:int;
            if (this._mouseDown)
            {
                xid = setInterval(function () : void
            {
                clearInterval(xid);
                jumpToReader();
                return;
            }// end function
            , 500);
            }
            else
            {
                try
                {
                    ExternalInterface.call(JSInterface.AS_JUMP);
                }
                catch (e:Error)
                {
                }
            }
            return;
        }// end function

        protected function leftKeyCode() : void
        {
            if (this.docLocatModel.pagesAllAvailable <= 0)
            {
                return;
            }
            if (DocConfig.pageMode == DocConfig.PAGE_MODE_SINGLE)
            {
                if (this.docLocatModel.pageth >= 1)
                {
                    var _loc_1:* = this.docLocatModel;
                    var _loc_2:* = this.docLocatModel.pageth - 1;
                    _loc_1.pageth = _loc_2;
                }
            }
            else if (this.docLocatModel.pageth > 0)
            {
                this.docLocatModel.pageth = this.docLocatModel.pageth - (this.docLocatModel.pageth == 1 ? (1) : (2));
            }
            dispatchEvent(new LocatEvent(LocatEvent.PAGE_LOADED));
            return;
        }// end function

        protected function rightKeyCode() : void
        {
            if (this.docLocatModel.pagesAllAvailable <= 0)
            {
                return;
            }
            if (DocConfig.pageMode == DocConfig.PAGE_MODE_SINGLE)
            {
                if (this.docLocatModel.pageth <= this.docLocatModel.pagesAllAvailable - 2)
                {
                    var _loc_1:* = this.docLocatModel;
                    var _loc_2:* = this.docLocatModel.pageth + 1;
                    _loc_1.pageth = _loc_2;
                }
            }
            else if (this.docLocatModel.pageth < (this.docLocatModel.pagesAllAvailable - 1))
            {
                this.docLocatModel.pageth = this.docLocatModel.pageth + (this.docLocatModel.pageth == this.docLocatModel.pagesAllAvailable - 2 ? (1) : (2));
            }
            dispatchEvent(new LocatEvent(LocatEvent.PAGE_LOADED));
            return;
        }// end function

        protected function stageMouseEvent(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            this._mouseDown = event.buttonDown;
            if (this._mouseDown)
            {
                this._pagethWhenMouseDown = this.docLocatModel.pageth;
            }
            else
            {
                _loc_2 = this.docLocatModel.pageth - this._pagethWhenMouseDown;
                if (_loc_2 != 0)
                {
                    WenkuLog.addLog(WenkuLog.LOGURL_DRAGPAGES + _loc_2);
                }
            }
            return;
        }// end function

        protected function doStageMouseWheelInFull(event:MouseEvent = null) : void
        {
            if (!this.screenModel.isFullscreen)
            {
                return;
            }
            var _loc_2:* = -event.delta || 0;
            this.doMouseWheel(_loc_2);
            return;
        }// end function

        protected function docKeyDown(event:KeyboardEvent = null) : void
        {
            if (this.docLocatModel.pagesAllAvailable <= 0)
            {
                return;
            }
            if (Config.isTextfieldActivated)
            {
                return;
            }
            var _loc_2:Number = 0;
            switch(event.keyCode)
            {
                case Keyboard.UP:
                case Keyboard.PAGE_UP:
                {
                    this.previousScreenButtonDown();
                    break;
                }
                case Keyboard.DOWN:
                case Keyboard.PAGE_DOWN:
                case Keyboard.SPACE:
                {
                    this.nextScreenButtonDown();
                    break;
                }
                case Keyboard.LEFT:
                {
                    this.leftKeyCode();
                    WenkuLog.addLog(WenkuLog.LOGURL_KEY_LEFT);
                    return;
                }
                case Keyboard.RIGHT:
                {
                    this.rightKeyCode();
                    WenkuLog.addLog(WenkuLog.LOGURL_KEY_RIGHT);
                    return;
                }
                default:
                {
                    break;
                }
            }
            this.thisViewer.vScrollPosition = this.thisViewer.vScrollPosition + _loc_2;
            return;
        }// end function

        protected function previousScreenButtonDown(event:MouseEvent = null) : void
        {
            if (event)
            {
                WenkuLog.addLog(WenkuLog.LOGURL_SCREENPAGEUP + "&pn=" + (this.docLocatModel.pageth + 1));
            }
            var _loc_2:* = this.thisViewer.vScrollPosition - (this.screenModel.stageHeight - Config.TB_HEIGHT - Config.SB_HEIGHT);
            TweenLite.killTweensOf(this.thisViewer);
            TweenLite.to(this.thisViewer, 0.5, {vScrollPosition:_loc_2});
            return;
        }// end function

        protected function nextScreenButtonDown(event:MouseEvent = null) : void
        {
            if (event)
            {
                WenkuLog.addLog(WenkuLog.LOGURL_SCREENPAGEDOWN + "&pn=" + (this.docLocatModel.pageth + 1));
            }
            var _loc_2:* = this.thisViewer.vScrollPosition + this.screenModel.stageHeight - Config.TB_HEIGHT - Config.SB_HEIGHT;
            TweenLite.killTweensOf(this.thisViewer);
            TweenLite.to(this.thisViewer, 0.5, {vScrollPosition:_loc_2});
            return;
        }// end function

        protected function zoomChanged(event:Event) : void
        {
            this.thisViewer.zoomNumber = this.zoomModel.zoomValues[this.zoomModel.zoomLevel];
            return;
        }// end function

        protected function getPageOffset() : int
        {
            return 0;
        }// end function

        protected function getPagethOnClose() : int
        {
            return (this.docLocatModel.pageth + 1);
        }// end function

        public function init() : void
        {
            this._searchPageth = -1;
            this.screenModel.stage.addEventListener(KeyboardEvent.KEY_UP, this.docKeyDown);
            this.screenModel.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.stageMouseEvent);
            this.screenModel.stage.addEventListener(MouseEvent.MOUSE_UP, this.stageMouseEvent);
            if (!Config.isAPIViewer)
            {
                try
                {
                    ExternalInterface.addCallback(JSInterface.JS_MOUSE_WHEEL, this.doMouseWheel);
                    ExternalInterface.addCallback(JSInterface.JS_KEY_LEFT, this.leftKeyCode);
                    ExternalInterface.addCallback(JSInterface.JS_KEY_RIGHT, this.rightKeyCode);
                    ExternalInterface.addCallback(JSInterface.JS_PAGE_OFFSET, this.getPageOffset);
                    ExternalInterface.addCallback(JSInterface.JS_PAGETH, this.getPagethOnClose);
                }
                catch (e:Error)
                {
                }
            }
            this.docLocatModel.init();
            return;
        }// end function

        public function isPagethLoaded(param1:int) : Boolean
        {
            return this.docLocatModel.isPagethLoaded(param1);
        }// end function

        public function doMouseWheel(param1:Number) : void
        {
            var delta:* = param1;
            try
            {
                delta = delta * 10;
                this.thisViewer.vScrollPosition = this.thisViewer.vScrollPosition + delta;
            }
            catch (err:Error)
            {
                Logger.error("doMouseWheel, no scrollpane");
            }
            return;
        }// end function

        public function switchPagesMode(param1:String) : void
        {
            if (!this.docLocatModel.pagethLoading)
            {
                return;
            }
            if (this.docPageFlip)
            {
                this.docPageFlip.unload();
            }
            this.thisViewer.destroy();
            switch(param1)
            {
                case DocConfig.PAGE_MODE_DOUBLE:
                {
                    this.thisViewer = InstanceManager.createSingletonInstance(DoubleViewer);
                    break;
                }
                case DocConfig.PAGE_MODE_SINGLE:
                {
                    this.thisViewer = InstanceManager.createSingletonInstance(this.loaderInfoVo.isPPT ? (PPTViewer) : (SingleViewer));
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            DocConfig.pageMode = param1;
            addChild(this.thisViewer as DisplayObject);
            this.thisViewer.data = this.docLocatModel.pagesArray;
            return;
        }// end function

        public function doSearch(param1:int, param2:String) : void
        {
            var id:int;
            var pageIndex:* = param1;
            var kywd:* = param2;
            this.closeSearchResult();
            id = setInterval(function () : void
            {
                clearInterval(id);
                doSearchDELAY(pageIndex, kywd);
                return;
            }// end function
            , 50);
            return;
        }// end function

        public function doSearchDELAY(param1:int, param2:String) : void
        {
            var _loc_6:Sprite = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:String = null;
            var _loc_10:int = 0;
            if (!param2 || param2 == "" || param1 == this._searchPageth)
            {
                return;
            }
            var _loc_3:Array = [];
            _loc_3 = param2.split(" ");
            var _loc_4:Number = 0;
            this._searchPageth = param1;
            var _loc_5:int = 0;
            while (_loc_5 < 3)
            {
                
                if (_loc_5 + param1 > (this.docLocatModel.pagesAllAvailable - 1))
                {
                    break;
                }
                if (!this.docLocatModel.pagesLoaderArray || !this.docLocatModel.pagesLoaderArray[_loc_5 + param1])
                {
                    this._resultNum = 0;
                    break;
                }
                _loc_6 = Loader(this.docLocatModel.pagesLoaderArray[_loc_5 + param1]).content as Sprite;
                this["_snapText" + _loc_5] = _loc_6.textSnapshot;
                _loc_7 = 0;
                _loc_8 = _loc_3.length;
                while (_loc_7 < _loc_8)
                {
                    
                    _loc_9 = _loc_3[_loc_7];
                    if (_loc_9 != "")
                    {
                        _loc_10 = this["_snapText" + _loc_5].findText(0, _loc_9, false);
                        if (_loc_10 == -1)
                        {
                        }
                        else if (_loc_10 >= 0)
                        {
                            do
                            {
                                
                                this["_snapText" + _loc_5].setSelectColor(Config.HIGHLIGHT_COLOR);
                                this["_snapText" + _loc_5].setSelected(_loc_10, _loc_10 + _loc_9.length, true);
                                _loc_10 = this["_snapText" + _loc_5].findText(_loc_10 + _loc_9.length, _loc_9, false);
                                _loc_4 = _loc_4 + 1;
                            }while (_loc_10 > 0)
                        }
                    }
                    _loc_7++;
                }
                _loc_5++;
            }
            this._resultNum = _loc_4;
            dispatchEvent(new LocatEvent(LocatEvent.SEARCH_RESULT));
            return;
        }// end function

        public function closeSearchResult() : void
        {
            this._searchPageth = -1;
            if (this._snapText0)
            {
                this._snapText0 = null;
            }
            if (this._snapText1)
            {
                this._snapText1 = null;
            }
            if (this._snapText2)
            {
                this._snapText2 = null;
            }
            return;
        }// end function

        public function get pageth() : int
        {
            return this.docLocatModel.pageth;
        }// end function

        public function set pageth(param1:int) : void
        {
            this.docLocatModel.pageth = param1;
            return;
        }// end function

        public function get pagesAllAvailable() : uint
        {
            return this.docLocatModel.pagesAllAvailable;
        }// end function

        public function get resultNum() : int
        {
            return this._resultNum || 0;
        }// end function

        public function get scrollPosition() : Number
        {
            return this.thisViewer.vScrollPosition;
        }// end function

        public function get scrollMax() : Number
        {
            return this.thisViewer.maxVScrollPosition;
        }// end function

    }
}
